package com.hengshan.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.hengshan.domain.entity.Menu;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * 权限表(Menu)表数据库访问层
 *
 * @author muxijun
 * @since 2023-12-22 15:42:58
 */
@Mapper
public interface MenuMapper extends BaseMapper<Menu> {

    @Results(id = "permsInfo", value = {@Result(column = "perms", property = "perms")})
    @Select("select distinct m.perms from user_role ur left join role_menu rm on ur.role_id=rm.role_id"
            + " left join menu m on rm.menu_id=m.id where ur.user_id=#{userId} and m.status=#{status} "
            +" and m.perms is not null and m.perms<>''")
    List<String> selectPermsByUserId(Long userId, int status);

    @Select("select distinct m.id, m.name, m.type, m.parent_id, m.order_num, m.visible, m.status, " +
            "m.is_frame, m.icon, m.component, m.perms, m.path " +
            "from user_role ur left join role_menu rm on ur.role_id=rm.role_id " +
            "left join menu m on rm.menu_id=m.id " +
            "where ur.user_id=#{userId} and m.status=#{status} order by m.parent_id,m.order_num asc")
    List<Menu> selectMenusByUserId(Long userId, int status);

    @Select({"<script>" +
            "select distinct m.id, m.name, m.type, m.parent_id, m.order_num, m.visible, m.status, " +
            "m.is_frame, m.icon, m.component, m.perms, m.path " +
            "from user_role ur left join role_menu rm on ur.role_id=rm.role_id " +
            "left join menu m on rm.menu_id=m.id " +
            "where ur.user_id=#{userId} and m.status=#{status} and m.type in" +
            "<foreach collection='menuTypes' item='type' open='(' separator=',' close=')'>#{type}</foreach> " +
            "order by m.parent_id,m.order_num asc" +
            "</script>"})
    List<Menu> selectMenusByUserIdAndType(Long userId, List<String> menuTypes, int status);

    @Select("select menu_id from role_menu where role_id=#{roleId}")
    List<Integer> selectMenuIdsByRoleId(Integer roleId);
}
